package com.banciyuan.bcywebview.biz.main.mineinfo.reward;

import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.GiftApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.WaitLightDialog;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.PayStatus;

/**
 * Created by squall on 2016/5/31.
 */
public class RewardSettingActivity extends BaseActivity {

    private ImageView switch_iv;
    private TextView intro;
    private RelativeLayout intro_line;
    private PayStatus payStatus;
    private RequestQueue queue;
    private WaitLightDialog waitLightDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);
        initArgs();
        initActionbar();
        initUi();
        initAction();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.feed_setting));
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(getContext());
    }

    @Override
    protected void initData() {
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + GiftApi.getSetting();
        Response.Listener onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, RewardSettingActivity.this)) {
                    try {
                        JSONObject result = new JSONObject(s);
                        Gson gson = new Gson();
                        payStatus = gson.fromJson(result.getString(HttpUtils.UPLOAD_DATA), PayStatus.class);
                        renderUi();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, RewardSettingActivity.this, params);
        StringRequest stringRequest = new StringRequestParam(Request.Method.POST, url, params, onSucc, onErrorProxy);
        queue.add(stringRequest);
    }

    private void renderUi() {
        if (payStatus.isPayment()) {
            switch_iv.setSelected(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_on, getTheme()));
            } else {
                switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
            }
        } else {
            switch_iv.setSelected(false);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_off, getTheme()));
            } else {
                switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
            }
        }

        if (payStatus.getIntro() != null) {
            intro.setText(Html.fromHtml(payStatus.getIntro()));
        }
    }

    @Override
    protected void initUi() {
        waitLightDialog = new WaitLightDialog(getContext(), R.style.BestDialog);
        switch_iv = (ImageView) findViewById(R.id.switch_iv);
        intro = (TextView) findViewById(R.id.feed_intro);
        intro_line = (RelativeLayout) findViewById(R.id.feed_line);
    }

    private void setFeed() {
        String url = HttpUtils.BASE_URL + GiftApi.getSetPayment();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair("is_open", switch_iv.isSelected() ? "0" : "1"));
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, getContext())) {
                    if (switch_iv.isSelected()) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_off, getTheme()));
                        } else {
                            switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
                        }
                        switch_iv.setSelected(false);
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_on, getTheme()));
                        } else {
                            switch_iv.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
                        }
                        switch_iv.setSelected(true);
                    }
                }
                if (!RewardSettingActivity.this.isFinishing()) {
                    waitLightDialog.dismiss();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (!RewardSettingActivity.this.isFinishing()) {
                    waitLightDialog.dismiss();
                }
            }
        };
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(requestParam);
    }

    @Override
    protected void initAction() {
        switch_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!waitLightDialog.isShowing()) {
                    waitLightDialog.show();
                    setFeed();
                }
            }
        });

        intro_line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (payStatus != null) {
                    gotoUtil.gotoAct(RewardSettingActivity.this, ChangeRewardActivity.class, payStatus);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
